<?php

namespace app\common\model;

use think\Db;

/**
 *  分类表
 */
class Categorys extends Base {

    /**
     * 获取列表
     */
    public function getAll($where = null) {
        return (Object) $this->where($where)->order('id', 'asc')->select()->toArray();
    }

    /**
     * 获取分类详情
     * $id分类id
     */
    public function getById($val,$field='id') {
        return (Object) $this->where($field, $val)->find()->toArray();
    }

    /**
     * 获取分类id
     * 
     */
    public function get_id($p_id) {
        $rs = $this->where('id', 'like', "{$p_id}__")->order("id desc")->find();
        if (empty($rs)) {
            $id = $p_id . '01';
        } else {
            $id = intval($rs['id']) + 1;
            if (strlen($id) % 2 != 0)
                $id = '0' . $id;
        }
        return $id;
    }

    /**
     * 修改node值
     * 下级数量
     */
    public function node_update($id) {
        $i = 2;
        while ($i != strlen($id) && $id != '') {
            $p_id = substr($id, 0, $i);
            $i +=2;
            $tmp = $this->where("id", 'like', "{$p_id}__")->select();
            $val = count($tmp);
            $this->where("id", $p_id)->update(['node' => $val]);
        }
    }

}
